import axios from 'axios';
// import qs from 'qs';
// import { Global, showLoading, hideLoading, showDialog } from './global'

const BASE_URL = '/api';  //测试地址

var http = axios.create({
    baseURL:BASE_URL,
    timeout:60000
})

//拦截请求
http.interceptors.request.use(
  config => {
      let token = localStorage.getItem('token');
      if(token){
        config.headers['Authorization'] = token;
      }
      return config;
  },error => {
      return Promise.error(error);
})

// 添加响应拦截器
http.interceptors.response.use(function (response) {
    // 根据返回的errno值来做不同的处理，需要和后端统一定义
    if(response.status === 200 && response.data.code){
      let errorMsg = '请求异常';
      switch(response.data.code){
        case 65001:
          errorMsg = '请求参数错误！';
          break;
        case 60001:
          errorMsg = 'Token验证失败！';
          break;
        case 65006:
          errorMsg = '您无权操作！';
          break;
        case 65008:
          errorMsg = '请求执行失败！';
          break;
        case 65001:
          errorMsg = '设备参数错误！';
          break;  
        case 65004:
          errorMsg = '房间信息错误！';
          break;
        case 65002:
          errorMsg = '设备异常！';
          break;
        case 64004:
          errorMsg = '执行异常！';
          break;        
        default: 
          errorMsg = response.data.message;
          break;
      }
      // showDialog(response.data.message);
      // Toast({ message: errorMsg });
    }
    return response;
  }, function (error) {
    
    if (error && error.response) {
      switch (error.response.status) {
        case 401:
          error.message = '未授权，请登录！';
          break

        case 403:
          error.message = '服务器拒绝请求，请重试！';
          break

        case 404:
          error.message = `请求地址出错: ${error.response.config.url}`;
          break

        case 408:
          error.message = '请求超时！';
          break

        case 500:
          error.message = '服务器内部错误，请刷新页面或重试操作！';
          break

        case 501:
          error.message = '服务未实现！';
          break

        case 502:
          error.message = '服务器网关错误，请刷新页面或重试操作！';
          break

        case 503:
          error.message = '服务暂时不可用，请稍后重试！';
          break

        case 504:
          error.message = '服务器网关超时，请刷新页面或重试操作！';
          break

        case 505:
          error.message = 'HTTP版本不受支持！';
          break
        default:
      }
      // showDialog(error.message);
    }
    // 对响应错误做点什么
    return Promise.reject(error);
  });

export default {
  //get请求
  get(url,param){
    return new Promise((resolve,reject)=>{
        const dataJson = JSON.stringify(param)
        http({
            method:'get',
            url: `${url}`,
            data:dataJson,
            params: param,
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(response => {
            resolve(response.data)
        }).catch(response => {
            reject(response)
        })
    })
  },
  //post请求
  post(url,param){
      return new Promise((resolve,reject)=>{
          const dataJson = JSON.stringify(param)
          http({
              method:'post',
              url: `${url}`,
              data:dataJson,
              headers: {
                  'Content-Type': 'application/json'
              }
          }).then(response => {
              resolve(response.data)
          }).catch(response => {
              reject(response)
          })
      })
  },
  postFormData(url,param){
      return new Promise((resolve,reject)=>{
          const dataJson = qs.stringify(param)
          http({
              method:'post',
              url: `${url}`,
              data:dataJson,
              headers: {
                  'Content-Type': 'application/x-www-form-urlencoded'
              }
          }).then(response => {
              resolve(response.data)
          }).catch(response => {
              reject(response)
          })
      })
  }
}